﻿using FRMS.BUS;
using FRMS.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AuditController : ControllerBase
    {
        private JwtSettings _jwtSettings;
        IHttpContextAccessor _access;
        public AuditController(IOptions<JwtSettings> jwtSettingsAccesser, IHttpContextAccessor access
        )
        {
            JwtSettings jwt = new JwtSettings();
            jwt.Audience = AppSettingsHelper.Configuration["JwtSettings:Audience"];
            jwt.Issuer = AppSettingsHelper.Configuration["JwtSettings:Issuer"];
            jwt.SecretKey = AppSettingsHelper.Configuration["JwtSettings:SecretKey"];
            _jwtSettings = jwt;
            _access = access;
        }

        /// <summary>
        /// 查询审核记录
        /// </summary>
        [HttpPost]
        [Route("QueryAuditList")]
        public ResponseMessage QueryAuditList(FrmsAuditInfo frmsAuditInfo)
        {
            ResponseMessage message = new ResponseMessage();
            AuditBUS BUS = new AuditBUS();
            try
            {
                message = BUS.QueryAuditList(frmsAuditInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询审核记录";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询审核记录异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 审核记录
        /// </summary>
        [HttpPost]
        [Route("UpdateAuditState")]
        public ResponseMessage UpdateAuditState(FrmsAuditInfo frmsAuditInfo)
        {
            ResponseMessage message = new ResponseMessage();
            AuditBUS BUS = new AuditBUS();
            try
            {
                message = BUS.UpdateAuditState(frmsAuditInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "同步审核记录";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "同步审核记录异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询关联业主
        /// </summary>
        [HttpPost]
        [Route("QueryOwner")]
        public ResponseMessage QueryOwner(FrmsAuditInfo frmsAuditInfo)
        {
            ResponseMessage message = new ResponseMessage();
            AuditBUS BUS = new AuditBUS();
            try
            {
                message = BUS.QueryAuditOwner(frmsAuditInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询关联业主";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询关联业主异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 关联业主
        /// </summary>
        [HttpPost]
        [Route("BindOwner")]
        public ResponseMessage BindOwner(FrmsAuditInfo frmsAuditInfo)
        {
            ResponseMessage message = new ResponseMessage();
            AuditBUS BUS = new AuditBUS();
            try
            {
                message = BUS.BindOwner(frmsAuditInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "关联业主";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "关联业主异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 新建业主且绑定
        /// </summary>
        [HttpPost]
        [Route("AddBindOwner")]
        public ResponseMessage AddBindOwner(OwnerInfo ownerInfo)
        {
            ResponseMessage message = new ResponseMessage();
            AuditBUS BUS = new AuditBUS();
            try
            {
                message = BUS.AddOwner(ownerInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "关联业主";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "关联业主异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 删除审核记录
        /// </summary>
        [HttpPost]
        [Route("DeleteAudit")]
        public ResponseMessage DeleteAudit(FrmsAuditInfo frmsAuditInfo)
        {
            ResponseMessage message = new ResponseMessage();
            AuditBUS BUS = new AuditBUS();
            try
            {
                message = BUS.DeleteAudit(frmsAuditInfo);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除审核记录";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除审核记录异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 查询二维码凭证
        /// </summary>
        [HttpPost]
        [Route("QueryQRVoucher")]
        public ResponseMessage QueryQRVoucher(int auditid)
        {
            ResponseMessage message = new ResponseMessage();
            AuditBUS BUS = new AuditBUS();
            try
            {
                message = BUS.QueryQRVoucher(auditid);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "查询凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "查询凭证异常";
                message.data = null;
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 新增二维码凭证
        /// </summary>
        [HttpPost]
        [Route("AddQRVoucher")]
        public ResponseMessage AddQRVoucher(QRVoucher qRVoucher)
        {
            ResponseMessage message = new ResponseMessage();
            AuditBUS BUS = new AuditBUS();
            try
            {
                message = BUS.AddQRVoucher(qRVoucher);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "新增二维码凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "新增二维码凭证异常";
                message.total = 0;
            }
            return message;
        }

        ///// <summary>
        ///// 修改二维码凭证
        ///// </summary>
        //[HttpPost]
        //[Route("UpdateQRVoucher")]
        //public ResponseMessage UpdateQRVoucher(QRVoucher qRVoucher)
        //{
        //    ResponseMessage message = new ResponseMessage();
        //    AuditBUS BUS = new AuditBUS();
        //    try
        //    {
        //        message = BUS.UpdateQRVoucher(qRVoucher);
        //    }
        //    catch (Exception e)
        //    {
        //        DataClearBUS dataClearBUS = new DataClearBUS();
        //        ErrorLog errorLog = new ErrorLog();
        //        errorLog.ErrorMsg = e.ToString();
        //        errorLog.Operation = "新增二维码凭证";
        //        dataClearBUS.AddErrorLog(errorLog);
        //        message.errcode = 1;
        //        message.errmsg = "新增二维码凭证异常";
        //        message.total = 0;
        //    }
        //    return message;
        //}

        /// <summary>
        /// 删除二维码凭证
        /// </summary>
        [HttpPost]
        [Route("DelQRVoucher")]
        public ResponseMessage DelQRVoucher(QRVoucher qRVoucher)
        {
            ResponseMessage message = new ResponseMessage();
            AuditBUS BUS = new AuditBUS();
            try
            {
                message = BUS.DelQRVoucher(qRVoucher);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "删除二维码凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "删除二维码凭证异常";
                message.total = 0;
            }
            return message;
        }

        /// <summary>
        /// 同步二维码凭证
        /// </summary>
        [HttpPost]
        [Route("SyncQRVoucher")]
        public ResponseMessage SyncQRVoucher(QRVoucher qRVoucher)
        {
            ResponseMessage message = new ResponseMessage();
            AuditBUS BUS = new AuditBUS();
            try
            {
                message = BUS.SyncQRVoucher(qRVoucher);
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "同步二维码凭证";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
                message.errmsg = "同步二维码凭证异常";
                message.total = 0;
            }
            return message;
        }
    }
}
